<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1100px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit || disableSave">
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>1、【项目问题】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectid" :rules="rules.validateRequire">
            <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="关联风险" prop="riskidentityid" :rules="rules.validateRequire">
            <el-input v-model="model.dispriskidentityid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmRiskIdentifyRefDialog" v-if="!disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="问题名称" prop="problemname" :rules="rules.validateRequire">
            <el-input v-model="model.problemname"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="问题类型" prop="problemtype" :rules="rules.validateRequire">
            <el-select v-model="model.problemtype" filterable clearable>
              <el-option v-for="item in codeModel.problemtypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="问题描述" prop="problemexplain" :rules="rules.validateRequire">
            <el-input v-model="model.problemexplain" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="提出日期" prop="proposedtime" :rules="rules.validateRequire">
            <el-date-picker v-model="model.proposedtime" type="date" placeholder="提出日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd" :readonly="true" class="is-disabled"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="问题状态" prop="problemstate" :rules="rules.validateRequire">
            <el-select v-model="model.problemstate" filterable clearable disabled>
              <el-option v-for="item in codeModel.problemstateList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>2、【问题处理】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="提出人" prop="proposedbyname" :rules="rules.validateRequire">
            <el-input v-model="model.proposedbyname" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProposedTeamRefDialog" v-if="!disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="提出人工号" prop="proposedbycode" :rules="rules.validateRequire">
            <el-input v-model="model.proposedbycode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="解决人" prop="resolvername" :rules="rules.validateRequire">
            <el-input v-model="model.resolvername" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmResolveTeamRefDialog" v-if="!disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="解决人工号" prop="resolvercode" :rules="rules.validateRequire">
            <el-input v-model="model.resolvercode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="闭环人" prop="closepeoplename" :rules="rules.validateRequire">
            <el-input v-model="model.closepeoplename" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmCloseTeamRefDialog" v-if="!disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="闭环人工号" prop="closepeoplecode" :rules="rules.validateRequire">
            <el-input v-model="model.closepeoplecode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="解决日期" prop="resolvetime">
            <el-date-picker v-model="model.resolvetime" type="date" placeholder="解决日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd" :disabled="true"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="解决情况" prop="resolveexplain">
            <el-input v-model="model.resolveexplain" type="textarea" :rows="2" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="闭环日期" prop="closetime">
            <el-date-picker v-model="model.closetime" type="date" placeholder="闭环日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd" :disabled="true"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="闭环描述" prop="closeexplain">
            <el-input v-model="model.closeexplain" type="textarea" :rows="2" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse>
        <el-collapse-item title="【单据】相关信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请部门" prop="deptname">
                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请公司" prop="orgname">
                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人" prop="username">
                <tl-ref-user v-model="model.username" :initDisplayData.sync="model.username" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人工号" prop="usercode">
                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate" :rules="rules.validateRequire">
                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交时间" prop="submitdate">
                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交时间" disabled></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审批流程" prop="flowid">
                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="state">
                <el-input v-model="model.dispstate" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <tl-approval-history ref="history" sourceType="PmProblems" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState<=2">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
        <svg-icon icon-class="save" />保存
      </el-button>
      <tl-attachment sourceType="PmProblems" :sourceId="model.id" :disabled="disableAttachmentButton" btnTitle="相关文件"></tl-attachment>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableDelApproval" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
    <tlpm-risk-identify-ref ref="pmRiskIdentifyRef" @confirm="getpmRiskIdentifyRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-risk-identify-ref>
    <tlpm-team-ref ref="pmTeamProposedRef" @confirm="getpmProposedTeamRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-team-ref>
    <tlpm-team-ref ref="pmTeamResolveRef" @confirm="getpmResolveTeamRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-team-ref>
    <tlpm-team-ref ref="pmTeamCloseRef" @confirm="getpmCloseTeamRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-team-ref>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList.vue'
import tlpmTeamRef from '@/views/ProjectManage/pmTeam/refList.vue'
import tlpmRiskIdentifyRef from '@/views/ProjectManage/pmRiskIdentify/refList.vue'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlpmProjectRef, tlpmTeamRef, tlpmRiskIdentifyRef },
  data() {
    return {
      moduleTitle: '项目问题',
      urlPrefix: '/pm/problems/',
      url: {
        // 额外的url
        queryOrgByDeptId: '/pm/resourceArchives/queryOrgByDeptId'
      },
      disableAttachmentButton: true
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ''
      }
    },
    editFlag: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        // if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
        //   this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        // }
        // this.$set(this.model, 'orgcode', this.org.code)
        // this.$set(this.model, 'orgname', this.org.name)
        // this.$set(this.model, 'deptcode', this.dept.code)
        // this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'proposedbycode', this.user.code)
        this.$set(this.model, 'proposedbyname', this.user.name)
        this.$set(this.model, 'proposedtime', new Date())
        this.$set(this.model, 'docdate', new Date())
        this.$set(this.model, 'problemstate', '1')
      }
    },
    afterDelApproval() {
      this.disableAttachmentButton = false
    },
    loaded() {
      if (this.isAdd === false && this.disableEditSubmit === true) {
        var params = {
          sourceType: 'PmProblems',
          sourceId: this.model.id
        }
        this.disableAttachmentButton = false
        getAction('/cbo/attachment/calculateCount', params).then((res) => {
          if (res.success) {
            if (res.result === 0) {
              this.disableAttachmentButton = true
              this.$emit('refresh')
            }
          }
        })
      }
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      this.$set(this.model, 'billuid', this.uid)
      postAction(this.url.add, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.queryData(res.result.id)
            this.disableAttachmentButton = false
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    onSelectDept(selectedDepts) {
      const selectedDept = selectedDepts[0]
      const param = {
        deptId: selectedDept.id
      }
      getAction(this.url.queryOrgByDeptId, param).then(res => {
        if (res.success) {
          const { code: orgCode, name: orgName } = res.result
          this.$set(this.model, 'orgcode', orgCode)
          this.$set(this.model, 'orgname', orgName)
        } else {
          this.$tap.alert('已选部门关联公司信息错误，请联系系统管理员')
        }
      })
    },
    checkSelectProject() {
      if (!this.model.projectid) {
        this.$message({
          message: '请先选择项目！',
          type: 'warning'
        })
        return false
      }
      return true
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      const { id, name } = selectionRow
      this.$set(this.model, 'projectid', id)
      this.$set(this.model, 'dispprojectid', name)
      this.$set(this.model, 'deptcode', selectionRow.ownerdeptcode)
      this.$set(this.model, 'deptname', selectionRow.ownerdeptname)
      this.$set(this.model, 'orgcode', selectionRow.ownerorgcode)
      this.$set(this.model, 'orgname', selectionRow.ownerorgname)
    },
    /** 打开关联风险参照 */
    openpmRiskIdentifyRefDialog() {
      if (this.checkSelectProject()) {
        this.$refs.pmRiskIdentifyRef.visible = true
      }
    },
    /** 获取关联风险参数数据 */
    getpmRiskIdentifyRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'riskidentityid', selectionRow.id)
      this.$set(this.model, 'dispriskidentityid', selectionRow.riskname)
    },
    /** 打开提出人参照 */
    openpmProposedTeamRefDialog() {
      if (this.checkSelectProject()) {
        this.$refs.pmTeamProposedRef.visible = true
      }
    },
    /** 获取提出人参数数据 */
    getpmProposedTeamRefData(selectionRow, selectKey, selectionValue) {
      /** this.$set(this.model, 'proposedbyname', selectionRow.id) */
      this.$set(this.model, 'proposedbyname', selectionRow.username)
      this.$set(this.model, 'proposedbycode', selectionRow.usercode)
    },
    /** 打开解决人参照 */
    openpmResolveTeamRefDialog() {
      if (this.checkSelectProject()) {
        this.$refs.pmTeamResolveRef.visible = true
      }
    },
    /** 获取解决人参数数据 */
    getpmResolveTeamRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'resolvername', selectionRow.username)
      this.$set(this.model, 'resolvercode', selectionRow.usercode)
    },
    /** 打开闭环人参照 */
    openpmCloseTeamRefDialog() {
      if (this.checkSelectProject()) {
        this.$refs.pmTeamCloseRef.visible = true
      }
    },
    /** 获取闭环人参数数据 */
    getpmCloseTeamRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'closepeoplename', selectionRow.username)
      this.$set(this.model, 'closepeoplecode', selectionRow.usercode)
    }
    // /** 重写提交审批方法*/
    // doSubmitApproval() {
    //     if (!this.url.submit) {
    //         this.$tap.alert('请设置审批地址!', 'warning')
    //         return
    //     }
    //     var tt = this.codeModel.problemstateList.filter((m) => m.code === this.model.problemstate)
    //     if (tt.length === 0) {
    //         this.$tap.alert('请选择问题状态', 'warning')
    //         return
    //     }
    //     if (tt[0].name !== '已解决') {
    //         this.$tap.alert('只有已解决问题才能提交审批', 'warning')
    //         return
    //     }
    //     this.submitLoading = true
    //     this.$tap
    //         .confirm('确定提交审批吗？提交后，单据内容不可修改！')
    //         .then((_) => {
    //             if (!this.beforeApproval()) {
    //                 return
    //             }
    //             putAction(this.url.submit, this.model).then((res) => {
    //                 if (res.success) {
    //                     this.$tap.message(res.message, 'success')
    //                     this.queryData(this.model.id)
    //                     this.afterApproval()
    //                 } else {
    //                     this.$tap.alert(res.message, 'warning')
    //                 }
    //                 this.submitLoading = false
    //             })
    //         })
    //         .catch((_) => {
    //             this.submitLoading = false
    //         })
    // }
  }
}
</script>
